package com.jeesite.modules.digitalize.service;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.jeesite.modules.digitalize.constants.OperationEnum;
import com.jeesite.modules.digitalize.util.response.ResultVo;
import com.jeesite.modules.digitalize.entity.DigitalizePlanDetail;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class DigitalizePlanTaskUtils {

    @Resource
    @Lazy
    DigitalizePlanDetailService digitalizePlanDetailService;

    private void setNewTime(Long id, Date newDate, String file) {
        UpdateWrapper wrapper = new UpdateWrapper();
        wrapper.eq("id", id);
        wrapper.set(file, newDate);
        digitalizePlanDetailService.update(wrapper);
    }

    public ResultVo setTime(Long id, OperationEnum com, Date newDate) {
        Integer count = digitalizePlanDetailService.lambdaQuery().eq(DigitalizePlanDetail::getParentId, id).count();
        if (count > 0) {
            return ResultVo.failure("父节点不能设置时间");
        }
        DigitalizePlanDetail current = digitalizePlanDetailService.getById(id);
        if (current == null) {
            return ResultVo.failure("节点计划任务不存在");
        }
        boolean update = false;
        switch (com) {
            case PLAN_START:
                update = DateUtil.compare(newDate, current.getPlanEndDate()) < 0;
                break;
            case PLAN_END:
                update = DateUtil.compare(newDate, current.getPlanStartDate()) > 0;
                break;
            case START:
                update = DateUtil.compare(newDate, current.getEndDate()) < 0;
                break;
            case END:
                update = DateUtil.compare(newDate, current.getStartDate()) > 0;
                break;
            default:
                break;
        }
        if (!update) {
            return ResultVo.failure("开始时间不能大于结束时间");
        }
        // 1. 更新本节点时间
        setNewTime(id, newDate, com.getCommand());
        // 更新父节点时间
        updateParentTime(current.getParentId(), newDate, com.getCommand());
        return ResultVo.success();
    }
    private Date getSelectDate(OperationEnum com, DigitalizePlanDetail current) {
        Date update = null;
        switch (com) {
            case PLAN_START:
                update = current.getPlanEndDate();
                break;
            case PLAN_END:
                update = current.getPlanStartDate();
                break;
            case START:
                update = current.getEndDate();
                break;
            case END:
                update = current.getStartDate();
                break;
        }
        return update;
    }

    private void updateParentTime(Long id, Date newDate, String file) {
        DigitalizePlanDetail parent = digitalizePlanDetailService.getById(id);
        // 为空或者相等不更新时间
        if (parent == null || DateUtil.compare(getSelectDate(OperationEnum.getByCommand(file), parent), newDate) == 0) {
            return;
        }
        Date date = null;
        if (OperationEnum.PLAN_START.getCommand().equals(file) || OperationEnum.START.getCommand().equals(file)) {
            date  = digitalizePlanDetailService.getBaseMapper().getMinStartDate(id, file);
        } else {
            date  = digitalizePlanDetailService.getBaseMapper().getMaxEndDate(id, file);
        }
        // 1. 更新本节点时间
        if (date == null) {
            return;
        }
        setNewTime(id, date, file);

        updateParentTime(parent.getParentId(), newDate, file);
    }

    public void setAllDate(Long pId) {
        Date ps = digitalizePlanDetailService.getBaseMapper().getMinStartDate(pId, OperationEnum.PLAN_START.getCommand());
        Date pe = digitalizePlanDetailService.getBaseMapper().getMinStartDate(pId, OperationEnum.PLAN_END.getCommand());
        Date s = digitalizePlanDetailService.getBaseMapper().getMinStartDate(pId, OperationEnum.START.getCommand());
        Date se = digitalizePlanDetailService.getBaseMapper().getMinStartDate(pId, OperationEnum.END.getCommand());
        if (ps != null || pe != null || s != null || se != null) {
            this.digitalizePlanDetailService.lambdaUpdate()
                    .eq(DigitalizePlanDetail::getId, pId)
                    .set(ps != null, DigitalizePlanDetail::getPlanStartDate, ps)
                    .set(pe != null, DigitalizePlanDetail::getPlanEndDate, pe)
                    .set(s != null, DigitalizePlanDetail::getStartDate, s)
                    .set(se != null, DigitalizePlanDetail::getEndDate, se)
                    .update();
        }
    }

    public void updateLeftTime(Long parentId, Long id) {
    //    更新父节点时间
        setAllDate(parentId);
    //   更新自己的时间
        setAllDate(id);
    }

    public void updateRightTime( Long id) {
        //    更新父节点时间
        setAllDate(id);
    }
}
